package mapspinnertests;

import java.util.logging.Logger;

import mapspinner.Terrain;

import com.jme.app.SimplePassGame;
import com.jme.input.FirstPersonHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.renderer.pass.RenderPass;
import com.jme.scene.Skybox;
import com.jme.scene.Spatial;
import com.jme.scene.Text;
import com.jme.scene.state.FogState;

public class TerrainTest extends SimplePassGame {
	private static final Logger logger = Logger.getLogger(TerrainTest.class
			.getName());

	public static void main(final String[] args) {
		final TerrainTest app = new TerrainTest();
		app.setConfigShowMode(ConfigShowMode.AlwaysShow);
		app.start();
	}

	private Terrain terrain;
	private Skybox skybox;
	private EditTest msst;
	private Text t;

	private void setupKeyBindings() {
		KeyBindingManager.getKeyBindingManager().set("E", KeyInput.KEY_E);
		t = Text.createDefaultTextLabel("Text", "Press E to test editing");
		t.setRenderQueueMode(Renderer.QUEUE_ORTHO);
		t.setLightCombineMode(Spatial.LightCombineMode.Off);
		t.setLocalTranslation(new Vector3f(0, 140, 1));
		statNode.attachChild(t);
	}

	@Override
	protected void simpleInitGame() {
		skybox = SkyBoxFactory.buildSkyBox(display);
		display.setTitle("Terrain Test");

		cam.setLocation(new Vector3f(0, -80, 0));
		final RenderPass skyPass = new RenderPass();
		skyPass.add(skybox);
		pManager.add(skyPass);
		rootNode.attachChild(skybox);

		final RenderPass rootPass = new RenderPass();
		rootPass.add(rootNode);
		pManager.add(rootPass);

		terrain = new Terrain(true, cam.getLocation(), pManager);

		cam.setFrustumPerspective(45.0f, (float) 4 / (float) 3, 1f, terrain
				.getSettings().getArrayWidth()
				/ 2
				* terrain.getSettings().getResolution()
				* terrain.getSettings().getHorizontalScale());

		rootNode.attachChild(terrain);

		final FogState fs = display.getRenderer().createFogState();
		fs.setDensity(0.5f);
		fs.setEnabled(true);
		fs.setColor(new ColorRGBA(0.5f, 0.5f, 0.5f, 0.5f));
		fs.setEnd(cam.getFrustumFar());
		fs.setStart(-1);
		fs.setDensityFunction(FogState.DensityFunction.ExponentialSquared);
		fs.setQuality(FogState.Quality.PerVertex);
		rootNode.setRenderState(fs);
		setupKeyBindings();
	}

	@Override
	protected void simpleRender() {
		super.simpleRender();
	}

	@Override
	protected void simpleUpdate() {
		super.simpleUpdate();
		if (msst != null)
			msst.update();
		skybox.updateGeometricState(0.0f, true);
		skybox.getLocalTranslation().set(cam.getLocation());
		if (KeyBindingManager.getKeyBindingManager().isValidCommand("E", false))
			if (msst == null) {
				statNode.detachChild(t);
				KeyBindingManager.getKeyBindingManager().set("E",
						KeyInput.KEY_E);
				t = Text.createDefaultTextLabel("Text",
						"Press E to stop editing");
				t.setRenderQueueMode(Renderer.QUEUE_ORTHO);
				t.setLightCombineMode(Spatial.LightCombineMode.Off);
				t.setLocalTranslation(new Vector3f(0, 140, 1));
				statNode.attachChild(t);
				((FirstPersonHandler) input).getMouseLookHandler().setEnabled(
						false);
				msst = new EditTest(rootNode, statNode, terrain
						.createBlockEditor(), display);
			} else {
				statNode.detachChild(t);
				KeyBindingManager.getKeyBindingManager().set("E",
						KeyInput.KEY_E);
				t = Text.createDefaultTextLabel("Text",
						"Press E to start editing");
				t.setRenderQueueMode(Renderer.QUEUE_ORTHO);
				t.setLightCombineMode(Spatial.LightCombineMode.Off);
				t.setLocalTranslation(new Vector3f(0, 140, 1));
				statNode.attachChild(t);
				msst = null;
			}
	}
}